﻿<template>
	<form-container
		:title="formData.id ? '编辑测试' : '增加测试'"
		:width="600"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="姓名" name="name">
				<a-input v-model:value="formData.name" placeholder="请输入姓名" allow-clear />
			</a-form-item>
			<a-form-item label="性别" name="sex">
				<a-radio-group v-model:value="formData.sex" placeholder="请选择性别" :options="sexOptions" />
			</a-form-item>
			<a-form-item label="民族" name="nation">
				<a-radio-group v-model:value="formData.nation" placeholder="请选择民族" :options="nationOptions" />
			</a-form-item>
			<a-form-item label="年龄" name="age">
				<a-input v-model:value="formData.age" placeholder="请输入年龄" allow-clear />
			</a-form-item>
			<a-form-item label="生日" name="bir">
				<a-date-picker
					v-model:value="formData.bir"
					value-format="YYYY-MM-DD HH:mm:ss"
					show-time
					placeholder="请选择生日"
					style="width: 100%"
				/>
			</a-form-item>
			<a-form-item label="排序码" name="sortCode">
				<a-input v-model:value="formData.sortCode" placeholder="请输入排序码" allow-clear />
			</a-form-item>
			<a-form-item label="存款" name="money">
				<a-input v-model:value="formData.money" placeholder="请输入存款" allow-clear />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</form-container>
</template>

<script setup name="genTestForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import genTestApi from '@/api/biz/genTestApi'

	// 抽屉状态
	const visible = ref(false)
	// 定义emit事件
	const emit = defineEmits({ successful: null })
	//表单
	const formRef = ref()
	// 表单数据，也就是默认给一些数据
	const formData = ref({})
	//提交等待
	const submitLoading = ref(false)

	//性别选项
	const sexOptions = ref([])
	//民族选项
	const nationOptions = ref([])

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true //显示抽屉
		//表单数据
		formData.value = {
			sortCode: 99
		}
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
			//const param = {
			//  id: record.id
			//}
			//genTestApi.genTestDetail(param).then((data) => {
			//  formData.value = Object.assign({}, data)
			//  })
		}
		sexOptions.value = tool.dictList('GENDER')
		nationOptions.value = tool.dictList('NATION')
	}

	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
	}

	// 默认要校验的
	const formRules = {}

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			genTestApi
				.submitForm(formDataParam, formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
